/**
 * Created by duwei on 2019/2/22.
 */
var ZiPai_ZongJieSuanWindow = ZongJieSuanWindow.extend({
    /**
     * 给ccsname赋值
     * @protected
     */
    _ccsFileName :res.ZiPai_ZongJieSuanWindow_json,
    /**
     * 点击时间
     * @private
     */
    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            'root.btn_return': '_clickReturn',
            'root.btn_share': '_clickShare'
            // 'btn_copy': '_clickCopy',
            // 'btn_sharelianjie': '_clickSharelianjie'
        };
    },
    /**
     * 初始化界面
     * @private
     */
    _initInfo: function () {
        var self = this;
        self.fenshu = "";
        self._setWanFa();
        self._setTime();
        self._setRoomId();
        self._setPlayers();
        self._setRowPos();
        self._setInfoText();
    },
    _clickShare:function () {
        var self = this;
        WeChatUtil.shareCapture(self);
    },
    /**
     * 设置玩法名称
     * @private
     */
    _setWanFa: function () {
        var self = this;
        var wanfa = (self._data['Map_id'] ? (gameData.mapId2Name(self._data['Map_id'])):null);
        this._nodeList["root.title"].setVisible(wanfa);
        if (this._nodeList["root.title"]) {
            this._nodeList["root.title"].setString(wanfa);
        }
        if(gameData.options){
            this._nodeList["root.fenbei"].setVisible(gameData.options['Fanbei']);
            var children = this._nodeList["root.fenbei"].getChildren();
            for (var i = 0; i < children.length; i++) {
                var fanbeiNode = children[i];
                fanbeiNode.setVisible(fanbeiNode.getName() == ('fanbei'+gameData.options['Fanbei']));
            }
        }
    },
    /**
     * 设置时间
     * @private
     */
    _setTime: function () {
        var self = this;
        var time = self._data['EndTime'].split(' ');
        this._nodeList["root.lb_ts"].setString(time[0]+'\n'+time[1]);
    },
    /**
     * 设置房间号/局数
     */
    _setRoomId: function () {
        var self = this;
        this._nodeList["root.txtRoomId"].setString('房号：' + gameData.roomId);
        this._nodeList["root.txtRounds"].setString('局数：' + self._data['CurrentRound'] + '/' + self._data['TotalRound']);
    },
    /**
     * 设置玩家数据
     * @private
     */
    _setPlayers: function () {
        var self = this;
        self.maxDianpao = 0;
        self.maxScore = 0;
        self.maxUid = 0;
        var players = self._data['Users'];
        for (var i = 0; i < players.length; i++) {
            var player = players[i];
            if (self.maxDianpao < player['dianpao']) {
                self.maxDianpao = player['dianpao'];
            }
            if (self.maxScore < player['Score']) {
                self.maxScore = player['Score'];
            }
        }
        for (var i = 0; i < players.length; i++) {
            var player = players[i];
            var uid = player['UserID'];
            var score = player['Score'];
            var playerInfo = gameData.getPlayerInfoByUid(uid);
            self.fenshu = self.fenshu + playerInfo.nickname + ":" + score + "\n";
            self._setRowUnit(i, player);
        }
        for (; i < 4; i++) {
            var RowUnit = this._nodeList["root.rowWidth.row" + i];
            if (RowUnit) {
                RowUnit.setVisible(false);
            }
        }
    },
    /**
     * 设置玩家牌数据
     * @param index
     * @param player
     * @private
     */
    _setRowUnit: function (index, player) {
        var self = this;
        var uid = player['UserID'] || player['uid'];
        var playerInfo = gameData.getPlayerInfoByUid(uid);
        self._setRowHead(index, playerInfo);
        self._setRowState(index, player);
        self._setScore(index, player);
        self._setScroll(index, player);
        self._setFangzhu(index, player);
    },
    /**
     * 设置玩家显示位置
     * @private
     */
    _setRowPos: function () {
        var self = this;
        var count = 1100;
        var midpoint = 0;
        if (this._nodeList['root.rowWidth']) {
            count = this._nodeList['root.rowWidth'].getContentSize().width;
            midpoint = this._nodeList['root.rowWidth'].getPositionX();
        }
        var players = self._data['Users'] || self._data['players'];
        var interval = count / players.length;
        var width = this._nodeList['root.rowWidth.row1'].getPositionX() - this._nodeList['root.rowWidth.row0'].getPositionX();
        if (players.length == 2) {
            interval = (count - 2 * width) / 3 + width
        }
        var originPosX = midpoint - (players.length - 1) / 2 * interval - width / 2;
        for (var s = 0; s < players.length; s++) {
            this._nodeList['root.rowWidth.row' + s].setPositionX(originPosX + interval * s);
        }
    },
    /**
     * 设置玩家头像
     * @param index
     * @param playerInfo
     * @private
     */
    _setRowHead: function (index, playerInfo) {
        var self = this;
        var RowUnit = self._nodeList["root.rowWidth.row" + index];
        RowUnit["txtName"].setString(ellipsisStr(playerInfo['nickname'], 6));
        RowUnit["txtID"].setString("ID:" + playerInfo['uid']);
        var oriUrl = gameData.getUserInfo(playerInfo['uid'])['headimgurl'];
        var url = decodeURIComponent(oriUrl || playerInfo['headimgurl']);
        loadHeadImage(url, RowUnit["head"] , true);//头像
    },
    /**
     * 设置状态
     * @param index
     * @param playerData
     * @private
     */
    _setRowState: function (index, playerData) {
        var self = this;
        var RowUnit = self._nodeList["root.rowWidth.row" + index];
        var score = playerData['Score'] || playerData['score'];
        RowUnit["dyj"].setVisible(self.maxScore <= score && score > 0);
        RowUnit["tuhao"].setVisible(false);
    },
    /**
     * 设置分数
     * @param index
     * @param playerData
     * @private
     */
    _setScore: function (index, playerData) {
        var self = this;
        var RowUnit = self._nodeList["root.rowWidth.row" + index];
        var score = playerData['Score'] || playerData['score'];
        RowUnit["lb_score_win"].setString(score);
        RowUnit["lb_score_fail"].setString(score);
        RowUnit["lb_score_win"].setVisible(score >= 0);
        RowUnit["lb_score_fail"].setVisible(score < 0);
        RowUnit["shu_bg"].setVisible(score < 0);
        RowUnit["ying_bg"].setVisible(score >= 0);
    },
    /**
     * 设置列表
     * @param index
     * @param playerData
     * @private
     */
    _setScroll: function (index, playerData) {
        var self = this;
        var RowUnit = self._nodeList["root.rowWidth.row" + index];
        var sv = RowUnit["sv"];
        var row0 = sv["row0"];
        var Scores = playerData['Scores'] || playerData['scores'];
        row0.setVisible(false);
        var totalheight = row0.getContentSize().height * Scores.length;
        if (totalheight < sv.getContentSize().height)
            totalheight = sv.getContentSize().height;
        sv.setInnerContainerSize(cc.size(sv.getContentSize().width, totalheight));
        for (var j = 1; j <= Scores.length; j++) {
            var row = sv["row" + j];
            if (!row) {
                row = duplicateNode(row0);
                row.setName('row' + j);
                sv.addChild(row);
            }
            row.setVisible(true);
            row.setPosition(0, totalheight - j * row0.getContentSize().height);

            row.getChildByName("txt_jushu").setString('第' + j + '局');
            var _score = Scores[j-1];
            _score = _score >= 0 ? ('+' + _score) : _score;
            row.getChildByName("txt_score").setString(_score);

            row.getChildByName("txt_jushu").setLocalZOrder(2);
            row.getChildByName("txt_score").setLocalZOrder(2);
        }
    },
    /**
     * 设置玩家房主
     * @param index
     * @param playerInfo
     * @private
     */
    _setFangzhu: function (index,playerInfo) {
        var self = this;
        var RowUnit = self._nodeList["root.rowWidth.row" + index];
        RowUnit['fz'].setVisible(false);
    },
    /**
     * 设置分享文本
     * @private
     */
    _setInfoText: function () {
        var self = this;
        this._infoText = "";
        var map_id = self._data['Map_id'] || self._data['map_id'];
        var wanfa = (map_id ? ('玩法：' + gameData.mapId2Name(map_id)) : '玩法：');
        var endTime = "结束时间：" + self._data['EndTime'];
        var dyjName = "大赢家:";
        var count = 0;
        for (var i = 0; i < self._data['Users'].length; i++) {
            var player = self._data['Users'][i];
            if(self.maxScore>0 && player['Score'] == self.maxScore){
                var playerInfo = gameData.getPlayerInfoByUid(player['UserID']);
                dyjName += count>0?(','+playerInfo['nickname']):playerInfo['nickname'];
                count++;
            }
        }
        this._infoText = this._infoText + wanfa + "\n" + endTime + "\n" + self.fenshu + (count>0?dyjName:"");
    },
});